package com.woniuxy.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.woniuxy.commen.ConnUtils;
import com.woniuxy.commen.Constansts;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class RoutePublisher {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接


        //建立信道
        Channel channel = ConnUtils.getconn();

        //(1)创建交换机,类型是DIRECT
        channel.exchangeDeclare(Constansts.ROUTE_EXCHANGE, BuiltinExchangeType.DIRECT);

        //创建队列移到消费者端执行

        //发送消息
        /*
         exchange 交换机
         routing-key 路由键
         properties 属性
         body 消息体
         */
        Scanner scanner = new Scanner(System.in);
        while(true) {
            print("请输入您要发送的消息");
            String message = scanner.next();
            print("请输入您的routing-key");
            String routingKey = scanner.next();
            if("exit".equals(message)){
                break;
            }
            //(2) 发给交换机，提供routing-key
            channel.basicPublish(Constansts.ROUTE_EXCHANGE, routingKey, null, message.getBytes("UTF-8"));
            print("[x] 消息发送成功！ " + message);
        }
    }

    private static void print(String str) {
        System.out.println("发布者："+str);
    }
}
